//
//  ConfigViC.m
//  Test
//
//  Created by fh on 2021/11/18.
//  Copyright © 2021 cashchina. All rights reserved.
//

#import "ConfigViC.h"
#import "InputView.h"
#import "StudioConfiguration.h"


@interface ConfigViC () <UITextFieldDelegate>
@property (nonatomic, strong) InputView *one;
@property (nonatomic, strong) InputView *two;
@property (nonatomic, strong) InputView *three;
@property (nonatomic, strong) InputView *four;
@property (nonatomic, strong) UIButton *backButton;
@end

@implementation ConfigViC

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]  removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotificationAction:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    [self.view addSubview:self.one];
    [self.view addSubview:self.two];
    [self.view addSubview:self.three];
    [self.view addSubview:self.four];
    [self.view addSubview:self.backButton];
    
    [self.one mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(64));
        make.left.equalTo(@(10));
        make.right.equalTo(@(-10));
    }];
    [self.two mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.one.mas_bottom).offset(10);
        make.left.equalTo(@(10));
        make.right.equalTo(@(-10));
    }];
    [self.three mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.two.mas_bottom).offset(10);
        make.left.equalTo(@(10));
        make.right.equalTo(@(-10));
    }];
    [self.four mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.three.mas_bottom).offset(10);
        make.left.equalTo(@(10));
        make.right.equalTo(@(-10));
    }];
    
    [self.backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-49));
        make.width.equalTo(@(80));
        make.height.equalTo(@(40));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
    
}

- (void)textFieldTextDidChangeNotificationAction:(NSNotification *)notification {
    UITextField *textfield = [notification object];
    if ([textfield isEqual:self.one.textField]) {
        [StudioConfiguration shareInstanceConfiguration].videoAverageBitRate = textfield.text.integerValue;
    }else if([textfield isEqual:self.two.textField]) {
        [StudioConfiguration shareInstanceConfiguration].videoExpectedSourceFrameRate = textfield.text.integerValue;
    }else if([textfield isEqual:self.three.textField]) {
        [StudioConfiguration shareInstanceConfiguration].videoWidth = textfield.text.integerValue;
    }else if([textfield isEqual:self.four.textField]) {
        [StudioConfiguration shareInstanceConfiguration].videoHeight = textfield.text.integerValue;
    }
}

- (void)backButtonAction:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Setter and getter
- (InputView *)one {
    if (!_one) {
        NSString *str = [NSString stringWithFormat:@"%ld",[StudioConfiguration shareInstanceConfiguration].videoAverageBitRate];
        _one = [[InputView alloc] initWithKey:@"码率:" value:str];
    }
    return _one;
}
- (InputView *)two {
    if (!_two) {
        NSString *str = [NSString stringWithFormat:@"%ld",[StudioConfiguration shareInstanceConfiguration].videoExpectedSourceFrameRate];
        _two = [[InputView alloc] initWithKey:@"帧速率:" value:str];
    }
    return _two;
}
- (InputView *)three {
    if (!_three) {
        NSString *str = [NSString stringWithFormat:@"%ld",[StudioConfiguration shareInstanceConfiguration].videoWidth];
        _three = [[InputView alloc] initWithKey:@"视频宽:" value:str];
    }
    return _three;
}
- (InputView *)four {
    if (!_four) {
        NSString *str = [NSString stringWithFormat:@"%ld",[StudioConfiguration shareInstanceConfiguration].videoHeight];
        _four = [[InputView alloc] initWithKey:@"视频高:" value:str];
    }
    return _four;
}
- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setTitle:@"返回" forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _backButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _backButton.backgroundColor = [UIColor orangeColor];
        _backButton.layer.cornerRadius = 5;
    }
    return _backButton;
}
@end
